<?php


namespace Single\Controllers;


use Framework\PBMessageRequest;
use Framework\PBMessageResponse;
use Phalcon\Mvc\Controller;

class BaseController extends Controller
{

    public function initialize()
    {

    }

    /**
     * @param $that
     * @throws \Exception
     */
    public function getReq($that)
    {
//            $input = $input
        $raw_post_data = file_get_contents('php://input', 'r');
        $req = new PBMessageRequest();
        $req->mergeFromString($raw_post_data);

        /**
         * @var $actionClass \Google\Protobuf\Internal\Message
         */
        $actionClassName = $that::ProtoBuf[$this->dispatcher->getActionName()];
        $actionClass = new $actionClassName();
        $actionClass->mergeFromString($req->getMessageData());
        return $actionClass;
    }

    /**
     * 设置返回数据
     * @param $rsp
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     */
    public function setRsp($rsp)
    {
        $str = $rsp->serializeToString();
//        echo $class->serializeToJsonString();
        $res = new PBMessageResponse();
        $res->setMessageData($str);
        $res->setResultCode(200);
        $res->setResultInfo('ok');
        $res->setType(1);
        $this->response->setContentType('application/octet-stream');
        $this->response->setContent($res->serializeToString());
        return $this->response;
    }
}